import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export default routes = new Router({
    routes: [
        {
            path: '/',
            name: 'Show',
            component: () => import('@/components/Show.vue'),
            children:[
                //人事工作台
                {
                    path: '/Personneldesk',
                    name: 'Personneldesk',
                    component: () => import('@/components/workbench/Personneldesk'),
                },
                //审批中心 待处理
                {
                    path: '/Pending',
                    name: 'Pending',
                    component: () => import('@/components/approver/Pending.vue'),
                },
                //财务工作台
                {
                    path: '/Financialworkbench',
                    name: 'Financialworkbench',
                    component:() => import('@/components/workbench/Financialworkbench')
                }
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/components/Login.vue'),
        }
    ],
    mode:"history"


})
// 为路由对象添加路由导航守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') {  //  如果用户访问的是登录页面 直接放行
      return next();
    }
  
    let token = window.localStorage.getItem('token');  //  从LocalStorage中得到token
    const tokenStartTime = window.localStorage.getItem('tokenStartTime');  //   获取存储token的开始时间
  
    const timeOver = 1 * 3600 * 100;  // 定义6个分钟过期，让用户重新登录一下
    let date = new Date().getTime();  //  当前时间
    if(date - tokenStartTime > timeOver) {  //  如果大于说明是token过期了
      token = null;
      window.localStorage.removeItem("token");
      window.localStorage.removeItem("tokenStartTime");
    }
  
    // 如果没有token值 那么就跳转到'/login
    if (!token) {
      if(to.path == '/login') {
        return next();
      }
      Message({
        message: '登录状态过期，请重新登录',
        type: 'warning'
      });
      return next('/login');
    }
  
    // 如果有token则放行
    next();
  })